package com.mylib.widget;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.mylib.libcore.R;

/**
 * @Author: cl
 * @CreateDate: 2021/2/26 9:54
 * @UpdateRemark: 自定义金币toast
 */
public class CustomCoinToast {
    private static TextView tv_coin,tv_title;

    public static void showToast(Context context, String message,String coinNum) {
        //加载Toast布局
        View toastRoot = LayoutInflater.from(context).inflate(R.layout.custom_coin_toast_layout, null);
        //初始化布局控件
        tv_coin = (TextView) toastRoot.findViewById(R.id.tv_coin);
        tv_title = (TextView) toastRoot.findViewById(R.id.tv_title);
        //为控件设置属性
        tv_title.setText(message+"");
        tv_coin.setText(coinNum+"");
        //Toast的初始化
        Toast toastStart = new Toast(context);
        //获取屏幕高度
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
    //    int width2 = outMetrics.widthPixels;
        int height = outMetrics.heightPixels;
       // int height = wm.getDefaultDisplay().getHeight();
        //Toast的Y坐标是屏幕高度的1/3，不会出现不适配的问题
        toastStart.setGravity(Gravity.TOP, 0, height / 3);
        toastStart.setDuration(Toast.LENGTH_LONG);
        toastStart.setView(toastRoot);
        toastStart.show();
    }
}
